package com.woniu.controller;

import com.woniu.constant.Constant;
import com.woniu.constant.HttpStatus;
import com.woniu.entity.Result;
import com.woniu.pojo.InspectorFeedback;
import com.woniu.service.InspectorService;
import com.woniu.utils.QiNiuUploadUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/inspectorFeedback")
public class InspectorFeedbackController {
    @Resource
    private InspectorService inspectorService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("巡检员上传意见反馈")
    @PostMapping("/feedBack")
    public Result createInspectorFeedback(@RequestBody InspectorFeedback inspectorFeedback){
        int flag = inspectorService.addInspectorFeedback(inspectorFeedback);
        return new Result(HttpStatus.SUCCESS,"新增成功");
    }

    @ApiOperation("巡检员意见反馈图片上传")
    @PostMapping("/img")
    public Result img(@RequestPart MultipartFile file) throws IOException {
        // 1. 拼接文件的名称
        String uuid = UUID.randomUUID().toString();
        String oldName = file.getOriginalFilename();
        String last = oldName.substring(oldName.lastIndexOf("."));
        String newFilename = uuid + last;
        // 2. 将用户上传的图片保存到七牛云
        QiNiuUploadUtils.uploadFile(file.getBytes(), newFilename);

        // 将图片的名称保存到Redis中
        stringRedisTemplate.opsForSet().add(Constant.REDIS_ALL_UPLOAD_IMAGE, newFilename);

        // 3. 需要将图片的名称响应给前端，做一个图片的回显
        return new Result(HttpStatus.SUCCESS,"图片上传成功！",newFilename);
    }

}
